<?php

namespace app\index\service;

use app\common\model\Consulting;
use app\common\model\LawCase;
use app\common\model\Trade;
use app\index\dao\ConsultingDao;
use app\index\dao\MemberDao;
use app\index\model\UM as UM;

/**
 * Class NumberService
 * @package app\index\service
 */
class NumberService
{
    // dao
    private MemberDao $memberDao;
    private ConsultingDao $consultingDao;

    public function __construct(MemberDao $memberDao, ConsultingDao $consultingDao)
    {
        $this->memberDao = $memberDao;
        $this->consultingDao = $consultingDao;
    }


    /**
     * 用户端-底部导航栏-红点
     * @param UM|null $user
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function numberOfMember(?UM $user): array
    {
        $message_num = 0;
        $order_num = 0;

        if ($user) {

            // 消息红点数
            $message_num = $this->countMessageNumber($user->id, $user->type);

            // 订单数红点
            $order_num = array_sum($this->numberOfOrder($user));
        }

        return [
            'message_num' => $message_num,
            'order_num' => $order_num,
        ];
    }

    /**
     * 律师端-底部导航栏-红点
     * @param UM|null $user
     * @return array
     */
    public function numberOfLawyer(?UM $user): array
    {
        $workbench_num = 0;
        $consulting_num = 0;
        $law_case_num = 0;
        $message_num = 0;

        if ($user) {

            // 工作台（我的咨询）
            $workbench_num = $this->countWorkbench($user->id);

            // 可抢单咨询
            $consulting_num = Consulting::where('matchType', 2)
                ->where('lawyerId', 0)
                ->where('status', 1)
                ->count();

            // 抢单案源
            $law_case_num = LawCase::where('isPublish', '1')
                ->where('publishTime', '<=', time())
                ->where('surplus', '>', 0)
                ->where('status', '<>', 2)
                ->count();

            // 消息红点数
            $message_num = $this->countMessageNumber($user->id, $user->type);

        }

        return [
            'workbench_num' => $workbench_num,
            'consulting_num' => $consulting_num,
            'law_case_num' => $law_case_num,
            'message_num' => $message_num,
        ];
    }


    /**
     * 订单-红点
     * @param UM|null $user
     * @return array
     */
    public function numberOfOrder(?UM $user): array
    {
        $number0 = 0;
        $number1 = 0;
        $number2 = 0;
        $number3 = 0;

        // 订单红点
        if ($user) {
            $id = $user->id;

            // 待付款
            $number0 = Trade::where('memberId', $id)
                ->where('payStatus', Trade::UNPAID)
                ->where('payExpTime', '>', time())
                ->count();

            // 进行中
            $number1 = Trade::where('memberId', $id)
                ->where('payStatus', Trade::PAID)
                ->where('refundStatus', Trade::NOTREFUND)
                ->where(fn($query) => $query->whereOr('endTime', '>', time())->whereNull('endTime', 'OR'))
                ->count();

            // 待评价
            $number2 = Trade::where('memberId', $id)
                ->where('payStatus', Trade::PAID)
                ->where('refundStatus', Trade::NOTREFUND)
                ->where('endTime', '<=', time())
                ->where('isComment', 0)
                ->count();

            // 退款
            $number3 = 0;
            // $number3 = Trade::where('memberId', $id)
            //     ->where('payStatus', Trade::PAID)
            //     ->whereIn('refundStatus', [Trade::REFUNDING, Trade::REFUNDED, Trade::REFUND_FAILED])
            //     ->count();

        }

        return [
            'number0' => $number0,
            'number1' => $number1,
            'number2' => $number2,
            'number3' => $number3,
        ];
    }

    /**
     * 消息红点数
     * @param $id
     * @param $type
     * @return int
     */
    private function countMessageNumber($id, $type): int
    {
        return $this->consultingDao->countMessageNumber($id, $type);
    }

    /**
     * 工作台红点数
     * @param $lawyerId
     * @return int
     */
    private function countWorkbench($lawyerId)
    {
        return $this->consultingDao->countWorkbench($lawyerId);
    }
}
